ifneq ($(MAKEFILET_MODULES),)
MAKEFILET_MODULES_HASHSUM = $(shell printf '%s\n' $(MAKEFILET_MODULES) | sha1sum | cut -f 1 -d " ")
MAKEFILET_MODULES_STORAGE_FILE ?= $(DIR_BUILD)/makefilet-modules/$(MAKEFILET_MODULES_HASHSUM).mk
MAKEFILET_MODULES_STORAGE_FILE_IF_EXISTS := $(wildcard $(MAKEFILET_MODULES_STORAGE_FILE))
MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE := $(dir $(MAKEFILET_MODULES_STORAGE_FILE))/partial-download
ifeq ($(MAKEFILET_MODULES_STORAGE_FILE_IF_EXISTS),)
# delete the file to be used for storing the downloaded modules
$(shell $(RM) $(MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE))
$(shell mkdir -p "$(dir $(MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE))")
MAKEFILET_MODULES_DOWNLOAD_FAILED_URLS := $(foreach module_url,$(MAKEFILET_MODULES),$(shell \
	echo >&2 "Downloading makefilet module: $(module_url)"; \
	echo "# module: $(module_url)" >>"$(MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE)"; \
	if ! wget --quiet --output-document - "$(module_url)" >>"$(MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE)"; then \
		echo >&2 "Failed to download $(module_url)"; \
		printf '%s\n' "$(module_url)"; \
	fi \
))
ifneq ($(strip $(MAKEFILET_MODULES_DOWNLOAD_FAILED_URLS)),)
$(error Some makefilet modules failed to downloaded: $(MAKEFILET_MODULES_DOWNLOAD_FAILED_URLS))
endif
$(shell mv "$(MAKEFILET_MODULES_PARTIAL_DOWNLOAD_FILE)" "$(MAKEFILET_MODULES_STORAGE_FILE)")
endif
include $(MAKEFILET_MODULES_STORAGE_FILE)
endif
